package org.example.stockdemo.controller;

import org.example.stockdemo.config.R;
import org.example.stockdemo.dto.StockGroupItemDTO;
import org.example.stockdemo.entity.StockDetail;
import org.example.stockdemo.entity.StockGroup;
import org.example.stockdemo.service.StockGroupItemService;
import org.example.stockdemo.service.StockGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/stockGroup")
public class StockGroupController {
    

    @Autowired
    private StockGroupService stockGroupService;
    
    @Autowired
    private StockGroupItemService stockGroupItemService;
    
    /**
     * 获取所有分组
     */
    @GetMapping("/groupList")
    public R<List<StockGroup>> getAllGroups() {
        return R.ok(stockGroupService.list());
    }
    
    /**
     * 创建分组
     */
    @PostMapping("/add")
    public R<Boolean> createGroup(@RequestBody StockGroup group) {
        return R.ok(stockGroupService.save(group));
    }
    
    /**
     * 更新分组
     */
    @PutMapping("/updateGroup/{id}")
    public R<Boolean> updateGroup(@PathVariable Long id, @RequestBody StockGroup group) {
        group.setId(id);
        return R.ok(stockGroupService.updateById(group));
    }
    
    /**
     * 删除分组
     */
    @DeleteMapping("/deleteGroup/{id}")
    public R<Boolean> deleteGroup(@PathVariable Long id) {
        return R.ok(stockGroupService.removeById(id));
    }
    
    /**
     * 获取分组中的股票
     */
    @GetMapping("/stocks/{groupId}")
    public R<List<StockDetail>> getStocksByGroup(@PathVariable Long groupId) {
        return R.ok(stockGroupItemService.getStocksByGroupId(groupId));
    }
    
    /**
     * 将股票添加到分组
     */
    @PostMapping("/addToGroup")
    public R<Boolean> addStockToGroup(@RequestBody StockGroupItemDTO dto) {
        return R.ok(stockGroupItemService.addStockToGroup(dto));
    }

    /**
     * 从分组中移除股票
     */
    @DeleteMapping("/remove/{groupId}/{stockCode}")
    public R<Boolean> removeStockFromGroup(@PathVariable Long groupId, @PathVariable String stockCode) {
        return R.ok(stockGroupItemService.removeStockFromGroup(stockCode, groupId));
    }
    
    /**
     * 移动股票到另一个分组
     */
    @GetMapping("/move/{fromGroupId}/{stockCode}/toGroupId")
    public R<Boolean> moveStockToGroup(@PathVariable Long fromGroupId,
                                               @PathVariable String stockCode,
                                               @RequestParam Long toGroupId) {
        return R.ok(stockGroupItemService.moveStockToGroup(stockCode, fromGroupId, toGroupId));
    }

    /**
     * 置顶股票
     */
    @GetMapping("/pushTop/{groupId}/{stockCode}")
    public R<Boolean> pushStockTop(@PathVariable Long groupId, @PathVariable String stockCode) {
        return R.ok(stockGroupItemService.pushStockTop(groupId,stockCode));
    }

    /**
     * 固定股票
     */
    @GetMapping("/fixedTop/{groupId}/{stockCode}")
    public R<Boolean> fixedStockTop(@PathVariable Long groupId, @PathVariable String stockCode) {
        return R.ok(stockGroupItemService.fixedStockTop(groupId,stockCode));
    }
}